<?php
session_start();
require_once 'confirmarAutent.php';

if(isset($_POST['oldPass'])){
    require_once 'database.php';
    $db = new DBTeamConnection();
    $oldPass = $_POST['oldPass'];
    $newPass = $_POST['newPass'];
    $confNewPass = $_POST['confNewPass'];
    
    $valid = strlen($oldPass) > 2 && strlen($newPass) > 2 && strlen($confNewPass) > 2 && $newPass == $confNewPass;
    
    $oldPass = hash("sha256", $oldPass);
    $newPass = hash("sha256", $newPass);
    
    if($valid){
        $valid = $db->alterarPassword($_SESSION['idUser'], $oldPass, $newPass);
    }
    
    if($valid){
        header("Location: http://localhost/Matematrix/password.php?update=true");
    } else {
        header("Location: http://localhost/Matematrix/password.php?update=false");
    }
    exit();
}

require_once 'alterarPassword.php';
?>
